import {
    CENTER_LIST_GET_REQUEST,
    CENTER_LIST_GET_SUCCESS,
    CENTER_LIST_GET_FAILURE,

    CENTER_DETAIL_GET_REQUEST,
    CENTER_DETAIL_GET_SUCCESS,
    CENTER_DETAIL_GET_FAILURE,

    SET_LIST_LOADING_STATUS
} from '../mutation-types';

//导入centerService 
import centerService from '../../services/centerService';


const getCenterList = ({ dispatch }, pageIndex) => {
    dispatch(SET_LIST_LOADING_STATUS, true);
    dispatch(CENTER_LIST_GET_REQUEST);
    
    return centerService.GetCenterList(pageIndex).then(res => {
        if (res.ok) {
            dispatch(CENTER_LIST_GET_SUCCESS, res.data, pageIndex);
            dispatch(SET_LIST_LOADING_STATUS, false);
        } else {
            dispatch(CENTER_LIST_GET_FAILURE);
            dispatch(SET_LIST_LOADING_STATUS, false);
        }
    });
}

const getCenterDetail = ({ dispatch }, centerId) => {
    dispatch(CENTER_DETAIL_GET_REQUEST);
    return centerService.GetCenterDetail(centerId).then(res => {
        if (res.ok) {
            dispatch(CENTER_DETAIL_GET_SUCCESS, res.data, centerId);
        } else {
            dispatch(CENTER_DETAIL_GET_FAILURE);
        }
    });
}


//导出 ,dispatch 由 mutation监听
export default {
    getCenterList,
    getCenterDetail
}
